<template>
	<sc-dialog v-model="visible" title="选择产线" show-fullscreen destroy-on-close width="750px" @close="close">
		<el-form ref="formRef" label-width="100px" :model="formData" :rules="rules">
			<el-form-item label="所属产线" prop="lineId">
				<el-select v-model="formData.lineId" placeholder="请选择产线">
					<el-option v-for="item in lineOptions" :key="item.id" :label="item.lineName" :value="item.id" />
				</el-select>
			</el-form-item>
		</el-form>

		<template #footer>
			<el-button @click="close">取 消</el-button>
			<el-button :loading="isSaveing" type="primary" @click="save">确 定</el-button>
		</template>
	</sc-dialog>
</template>

<script>
export default {
	emits: ['complete'],
	data() {
		return {
			isSaveing: false,
			visible: false,
			lineOptions: [],
			woItems: [],
			formData: {
				id: 0,
				lineId: undefined,
			},
			rules: {
				lineId: [{ required: true, message: "请选择产线", trigger: "blur" }],
			},
		};
	},
	methods: {
		async open(items) {
			this.woItems = items;
			const resp = await this.$API.mdline.list.get();
			this.lineOptions = resp.data;
			this.visible = true;
		},
		save() {
			this.$refs.formRef.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;

					const selected = this.lineOptions.find(s => s.id === this.formData.lineId);
					const lineCode = selected.lineCode;
					const input = this.woItems.map(s => ({
						woId: s.id,
						wo: s.wo,
						lineId: this.formData.lineId,
						lineCode: lineCode,
					}));
					const resp = await this.$API.prodwoschedule.schedule.post(input);
					this.isSaveing = false;
					if (resp.code == 200) {
						this.$emit("complete");
						this.visible = false;
						this.$message.success("排产成功");
					} else {
						this.$alert(resp.message, "提示", { type: "error" });
					}
				}
			});
		},
		close() {
			this.lineOptions = [];
			this.woItems = [];
			this.formData = {
				id: 0,
				lineId: undefined,
			};
			this.$refs.formRef.resetFields();
			this.visible = false;
		},
	},
};
</script>
